from rest_framework.viewsets import ModelViewSet, ViewSet
from apps.users.authentication import CustomAuthentication
from apps.users.permissions import CustomIsStaff
from apps.classes.models import Class
from apps.classes.serializers import ClassModelSerializer, UpdateClassModelSerializer
from rest_framework.response import Response
from rest_framework import status
from apps.classes.pagination import ClassPageNumberPagination


# Create your views here.
class ClassesViewSet(ModelViewSet):
    authentication_classes = [CustomAuthentication]
    permission_classes = [CustomIsStaff]
    pagination_class = ClassPageNumberPagination

    # 指明过滤、搜索、支持的排序字段
    filterset_fields = ['stage_no', 'course_id', 'speaker_id', 'head_id', 'state']
    ordering_fields = ['id']

    queryset = Class.objects.all().order_by('-stage_no', '-start_date')
    serializer_class = ClassModelSerializer

    def get_serializer_class(self):
        if self.request.method == 'PATCH':
            return UpdateClassModelSerializer
        return super().get_serializer_class()


class ClassesDescViewSet(ViewSet):
    authentication_classes = [CustomAuthentication]
    permission_classes = [CustomIsStaff]

    def get_state_info(self, request):
        states = dict()
        for code, state in Class.STATE_CHOICES:
            states[code] = state
        return Response({"message": "获取班级状态描述信息成功!", "data": states}, status=status.HTTP_200_OK)

    def get_teach_method(self, request):
        methods = dict()
        for code, method in Class.TEACHMETHOD_CHOICES:
            methods[code] = method
        return Response({"message": "获取班级授课方式描述信息成功!", "data": methods}, status=status.HTTP_200_OK)

    def get_school_time(self, request):
        times = dict()
        for code, time in Class.SCHOOLTIME_CHOICES:
            times[code] = time
        return Response({"message": "获取班级授课时间描述信息成功!", "data": times}, status=status.HTTP_200_OK)
